import axios from "axios";
import { Message } from "element-ui/lib";
// import store from '@/store'
import { Toast } from "vant";
import router from "../router";
import qs from "qs";

export const restApi = ({
  url,
  method = "get",
  data = null,
  params = null,
  headType = null,
  uploadSts = null,
}) => {
  // if (loading) {
  //   store.dispatch('loading/loading', 1)
  // }

  let headers = { token: getToken(), "Content-Type": "application/json" };
  let responseType = "";
  if (headType) {
    const _type = headType.toLocaleLowerCase();
    switch (_type) {
      case "formdata":
        headers = {
          token: getToken(),
          "Content-Type": "multipart/form-data",
        };
        data = formData(data);
        break;
      case "form":
        headers = {
          token: getToken(),
          "Content-Type": "application/x-www-form-urlencoded",
        };
        data = `request=${data.request}`;
        break;
      case "upload":
        headers = {
          token: getToken(),
          "Content-Type": "image/jpg",
          "x-oss-meta-author": "longfengshan",
        };
        break;
      case "mp4":
        headers = {
          token: getToken(),
          "Content-Type": "audio/mp4",
          "x-oss-meta-author": "longfengshan",
        };
        break;
      case "uploadsts":
        headers = {
          token: getToken(),
          tempToken: uploadSts,
        };
        break;
      case "json":
        headers = {
          responseType: "blob",
          token: getToken(),
          "Content-Type": "application/json",
        };
        responseType = "blob";
        break;
      case "buffer":
        headers = {
          responseType: "arraybuffer",
          token: getToken(),
          "Content-Type": "application/json",
        };
        responseType = "arraybuffer";
        break;
      default:
        break;
    }
  }
  return axios({
    headers,
    method,
    url,
    data,
    responseType,
    params,
    paramsSerializer: (params) => {
      return qs.stringify(params, { indices: false });
    },
  })
    .then((response) => {
      return response.data;
    })
    .catch(async (e) => {
      if (e.response) {
        const status = e.response.status;
        switch (status) {
          case 401:
            console.dir(e);
            // router.push("/login");
            localStorage.clear();
            break;
          case 400:
            if (e.response.data.resultMsg) {
              Toast.fail(e.response.data.resultMsg);
              // Message.error(e.response.data.resultMsg);
            } else {
              Toast.fail(JSON.stringify(e.response));
              // Message.error(JSON.stringify(e.response));
            }
            console.dir(e);
            break;
          case 403:
            console.dir(e);
            Toast.fail(JSON.stringify(e.response));
            // Message.error(JSON.stringify(e.response));
            break;
          case 404:
            console.dir(e);
            Toast.fail("接口404了，问后端咋回事");
            // Message.error("接口404了，问后端咋回事");
            break;
          default:
            if (e.response && e.response.data && e.response.data.resultMsg) {
              // return Message.error(e.response.data.resultMsg.toString());
              return Toast.fail(e.response.data.resultMsg.toString());
            }
            console.dir(e);
            // Message.error(e.toString());
            Toast.fail(e.toString());
        }
      } else {
        console.dir(e);
        Toast.fail(e.toString());
        // Message.error(e.toString());
      }
      return null;
    });
};
const formData = (arg) => {
  var _obj = new FormData();
  for (const x in arg) {
    if (arg[x] instanceof Array) {
      arg[x].forEach((item) => {
        return _obj.append(x, item);
      });
    } else {
      _obj.append(x, arg[x]);
    }
  }
  return _obj;
};
const getToken = () => {
  const token = localStorage.getItem("token");
  if (token) {
    return token;
  }
  return null;
};
export default restApi;
